#include "handlerupdate.h"
#include "Aria.h"
#include "ArNetworking.h"

handlerUpdate::handlerUpdate(QObject *parent,ArClientBase *client) :
    QObject(parent),
    updateNumbersCB(this,&handlerUpdate::updateNumbers),
    updateStringsCB(this,&handlerUpdate::updateStrings),
    updateBatteryInfoCB(this,&handlerUpdate::updateBatteryInfo)
{


    handledClient = client;

    if(!handledClient->dataExists("updateNumbers"));
    else{
        handledClient->addHandler("updateNumbers", &updateNumbersCB);
        handledClient->request("updateNumbers",50);
    }

    if(!handledClient->dataExists("updateStrings"));
    else{
        handledClient->addHandler("updateStrings", &updateStringsCB);
        handledClient->request("updateStrings",500);
    }

    if(!handledClient->dataExists("batteryInfo"));
    else{
        handledClient->addHandler("batteryInfo", &updateBatteryInfoCB);
        handledClient->requestOnce("batteryInfo");
        handledClient->request("batteryInfo",5000);
    }

}

void handlerUpdate::updateNumbers(ArNetPacket *packet){
    char temp[512];
    int len=512;
    double X,Y,theta,battery;
    int vel,rot_vel;

    battery = ((double)(packet->bufToByte2())/10.0);

    X = packet->bufToByte4();
    Y = packet->bufToByte4();


    theta = packet->bufToByte2();
    vel = packet->bufToByte2();
    rot_vel = packet->bufToByte2();

   emit signalUpdateNumbers(battery,X,Y,theta,vel,rot_vel);
}


void handlerUpdate::updateStrings(ArNetPacket *packet){

    char mode[512],status[512];
    int len=512;

    packet->bufToStr(status,len);
    packet->bufToStr(mode,len);


    emit signalUpdateStrings(mode,status);

}

void handlerUpdate::updateBatteryInfo(ArNetPacket *packet){

    double warning,shutdown;


    warning = packet->bufToDouble();
    shutdown = packet->bufToDouble();

    printf("Warning: %f",warning);
    printf("Shutdown: %f",shutdown);

    emit signalUpdateBatteryInfo(warning,shutdown);

}
